<?php
namespace app\admin\controller;

class Addons extends Admin{
    public function __construct(){
        parent::__construct();
        $this->assign("model","Addons");
    }
    public function data_save(){
        $model = input("post.model");
        $db = model($model);
        $postData = input("post.postData");
        parse_str($postData,$data);

        $data['info']['name']   =   trim($data['info']['name']);
        $addonFile              =   $this->preview($data,false);
        $addons_dir             =   ROOT_PATH . "addons/";
        //创建目录结构
        $files          =   array();
        $addon_dir      =   "$addons_dir{$data['info']['name']}/";
        $files[]        =   $addon_dir;
        $addon_name     =   "{$data['info']['name']}.php";
        $files[]        =   "{$addon_dir}{$addon_name}";
        if($data['has_config'] == 1);//如果有配置文件
            $files[]    =   $addon_dir.'config.php';

        if($data['has_outurl']){
            $files[]    =   "{$addon_dir}controller/";
            $files[]    =   "{$addon_dir}controller/{$data['info']['name']}.php";
        }
        $custom_config  =   '';//trim($data['custom_config']);
        if($custom_config)
            $data[]     =   "{$addon_dir}{$custom_config}";

        $custom_adminlist = '';//trim($data['custom_adminlist']);
        if($custom_adminlist)
            $data[]     =   "{$addon_dir}{$custom_adminlist}";

        create_dir_or_files($files);

        //写文件
        file_put_contents("{$addon_dir}{$addon_name}", $addonFile);
        if($data['has_outurl']){
            $addonController = <<<str
<?php

namespace addons\\{$data['info']['name']}\controller;
use think\addons\Controller;

class {$data['info']['name']} extends Controller{

}

str;
            file_put_contents("{$addon_dir}controller/{$data['info']['name']}.php", $addonController);
        }

        if($data['has_config'] == 1 && isset($data['config']))
            file_put_contents("{$addon_dir}config.php", $data['config']);


        $id = $db->save_data($data['info']);
        if($id === false){
            return json(['status' => 0,'msg'=>$db->getError()]);
        }
        return json(['status' => 1,'msg'=>'操作成功']);
    }
    //预览
    public function preview($data,$output = true){
        $data['info']['status'] =   (int)$data['info']['status'];
        $extend                 =   array();
        $custom_config          =   '';//trim($data['custom_config']);
        if($data['has_config'] && $custom_config){
            $custom_config = <<<str


        public \$custom_config = '{$custom_config}';
str;
            $extend[] = $custom_config;
        }

        $admin_list = '';//trim($data['admin_list']);
        if(isset($data['has_adminlist']) && $data['has_adminlist'] && $admin_list){
            $admin_list = <<<str


        public \$admin_list = [
            {$admin_list}
        ];
str;
           $extend[] = $admin_list;
        }

        $custom_adminlist = '';//trim($data['custom_adminlist']);
        if(isset($data['has_adminlist']) && $data['has_adminlist'] && $custom_adminlist){
            $custom_adminlist = <<<str


        public \$custom_adminlist = '{$custom_adminlist}';
str;
            $extend[] = $custom_adminlist;
        }

        $extend = implode('', $extend);
        $hook = '';
        if(isset($data['hook'])){
	        foreach ($data['hook'] as $value) {
	            $hook .= <<<str
	        //实现的{$value}钩子方法
	        public function {$value}(\$param){

	        }

str;
	        }
	    }

        $tpl = <<<str
<?php

namespace addons\\{$data['info']['name']};
use think\Addons;

/**
 * {$data['info']['title']}插件
 * @author {$data['info']['author']}
 */

    class {$data['info']['name']} extends Addons{

        public \$info = array(
            'name'=>'{$data['info']['name']}',
            'title'=>'{$data['info']['title']}',
            'description'=>'{$data['info']['description']}',
            'status'=>{$data['info']['status']},
            'author'=>'{$data['info']['author']}',
            'version'=>'{$data['info']['version']}'
        );{$extend}

        public function install(){
            return true;
        }

        public function uninstall(){
            return true;
        }

{$hook}
    }
str;
        if($output)
            exit($tpl);
        else
            return $tpl;
    }
}